# coding=utf-8
#import sys
#sys.path.append(r'H:/RareWolf/maya_2018/MainUI')

import maya.cmds as cmds
import maya.mel as mel
import MainUI.Menu as Menu
#import pymel.core as pm
import maya.utils as utils
from maya_umbrella import MayaVirusDefender
import os,shutil,sys
import shutil
#import pipeline_db

#pipeline_projectName = ''
#def userSetupProc( projectName):
'''
def projectNameCH2US( CH_Name):
	if(CH_Name == "B8_SR"):
		return "VW_NewTouarge_B8&SR_ANI"
	elif (CH_Name == "WK"):
		return "UE4_WK"
	elif(CH_Name == "SG"):
		return "Dota_CG"
	elif(CH_Name == "BYL"):
		return "BoYuan_Road_UE4_5G"
	elif(CH_Name == "UUtest"):
		return "UE_ProTest"
	elif(CH_Name == "JYY2"):
		return "JYY2"
	elif(CH_Name == "YS"):
		return "YS"
	elif(CH_Name == "大唐"):
		return "DT"
	elif(CH_Name == "DT"):
		return "DT"
	elif(CH_Name == "AHZ"):
		return "AHZ"
	elif(CH_Name == "SENM"):
		return "SENM"
	elif(CH_Name == "BMSW_S1"):
		return "BMSW_S1"
	elif(CH_Name == "HF"):
		return "HF"
	return ''
'''

#cmds.setAttr ("defaultResolution.w",1920)
#cmds.setAttr ("defaultResolution.h",1080)
#cmds.setAttr ("defaultResolution.dar",1.7777777910232544)
#cmds.playbackOptions( minTime='101', maxTime='130' )
#print '********************************************'
'''
def permissions(folder_name):
	if os.path.exists(folder_name):
		current_permissions = os.stat(folder_name).st_mode
		# 移除只读位
		new_permissions = current_permissions | 0o222
		# 修改文件权限
		os.chmod(folder_name, new_permissions)
		os.remove(folder_name)
def Shelf_Tab():
	# 获取环境变量
	#maya_shelf_path = os.path.join(os.environ.get('MAYA_SHELF_PATH').replace(";",""),"shelf_RareWolf_Ani.mel")
	maya_shelf_path ="H:/RareWolf_Toolkit/shelves/shelf_RareWolf_Ani.mel"
	home_dir = os.path.expanduser('~')
	if cmds.about(f = 1).count("2018"):
		folder_name = os.path.join(home_dir,"maya","2018","prefs","shelves","shelf_RareWolf_Ani.mel")
		permissions(folder_name)
		shutil.copy2(maya_shelf_path, folder_name)
	if cmds.about(f = 1).count("2019"):
		folder_name = os.path.join(home_dir,"maya","2019","prefs","shelves","shelf_RareWolf_Ani.mel")
		permissions(folder_name)
		shutil.copy2(maya_shelf_path, folder_name)
	if cmds.about(f = 1).count("2020"):
		folder_name = os.path.join(home_dir,"maya","2020","prefs","shelves","shelf_RareWolf_Ani.mel")
		permissions(folder_name)
		shutil.copy2(maya_shelf_path, folder_name)
	if cmds.about(f = 1).count("2021"):
		folder_name = os.path.join(home_dir,"maya","2021","prefs","shelves","shelf_RareWolf_Ani.mel")
		permissions(folder_name)
		shutil.copy2(maya_shelf_path, folder_name)
	if cmds.about(f = 1).count("2023"):
		folder_name = os.path.join(home_dir,"maya","2023","prefs","shelves","shelf_RareWolf_Ani.mel")
		permissions(folder_name)
		shutil.copy2(maya_shelf_path, folder_name)
	if cmds.about(f = 1).count("2024"):
		folder_name = os.path.join(home_dir,"maya","2024","prefs","shelves","shelf_RareWolf_Ani.mel")
		permissions(folder_name)
		shutil.copy2(maya_shelf_path, folder_name)
	if cmds.about(f = 1).count("2025"):
		folder_name = os.path.join(home_dir,"maya","2025","prefs","shelves","shelf_RareWolf_Ani.mel")
		permissions(folder_name)
		shutil.copy2(maya_shelf_path, folder_name)
	if cmds.about(f = 1).count("2026"):
		folder_name = os.path.join(home_dir,"maya","2026","prefs","shelves","shelf_RareWolf_Ani.mel")
		permissions(folder_name)
		shutil.copy2(maya_shelf_path, folder_name)
	
	print("---------------------------------------------")
'''
if cmds.pluginInfo ("renderSetup.py",q=1 ,l=1 ):
	cmds.evalDeferred('cmds.unloadPlugin ("renderSetup.py")')
def maya_default_config_run():
	MayaVirusDefender().setup()
	print(u'-----------------------成功加载 保护伞-----------------------')
def delunknow():
	unknownNodeList = cmds.ls(type="unknown")
	if unknownNodeList:
		cmds.lockNode(unknownNodeList, l=0)
		cmds.delete(unknownNodeList)
	unknownDagList = cmds.ls(type="unknownDag")
	if unknownDagList:
		cmds.delete(unknownDagList)
	loaded_plugins = cmds.unknownPlugin(query=True, list=True)
	if loaded_plugins:
		for plugin in loaded_plugins:
			cmds.unknownPlugin(plugin, remove=True)
	print(u"清除")

def CleanFile():
	myScriptDir = cmds.internalVar(userAppDir=True) + "scripts/"
	#if myScriptDir not in sys.path:
		#sys.path.append(myScriptDir)
	Vaccine = cmds.objExists('vaccine_gene')
	if(Vaccine==True):
		cmds.delete('breed_gene')
		cmds.delete('vaccine_gene')
		mmp01 = myScriptDir + "userSetup.py"
		mmp02 = myScriptDir + "vaccine.py"
		mmp03 = myScriptDir + "vaccine.pyc"
		if os.path.exists(mmp01):
			os.remove(mmp01)
		if os.path.exists(mmp02):
			os.remove(mmp02)
		if os.path.exists(mmp03):
			os.remove(mmp03)
		cmds.warning(u"!!!!!A:文件中 感染代码已清除>>>> ")
	else:
		cmds.warning( u'未发现病毒')
	'''
	for item in pm.lsUI(editors=True):
		if isinstance(item, pm.ui.ModelEditor):
			pm.modelEditor(item, edit=True, editorChanged="")
	'''
def DelayedStart():
	#Shelf_Tab()
	if cmds.about(f = 1).count("2018"):
		if not cmds.pluginInfo ("AnimSchoolPicker_2018.mll",q=1 ,l=1 ):
			cmds.evalDeferred('cmds.loadPlugin ("AnimSchoolPicker_2018.mll")')
	if not cmds.pluginInfo ("sceneCleaner.py",q=1 ,l=1 ):
		cmds.evalDeferred('cmds.loadPlugin ("sceneCleaner.py")')

	cmds.evalDeferred("maya_default_config_run()")

	cmds.evalDeferred('Menu.Menu_Min().Menu()')#启动菜单栏



	#cmds.evalDeferred('CleanFile()')
	#print '********************************************'
	cmds.scriptJob(event=["SceneOpened", "delunknow()"], protected=True)#打开文件时清除未知节点
	TipsVir = cmds.scriptJob(event=["SceneSaved", "CleanFile()"], protected=True)#保存文件时清除病毒
	#if not cmds.commandPort(":4434", query=True):
	#	cmds.commandPort(name=":4434")
	cmds.about(f = 1)

	if not cmds.pluginInfo ("redshift4maya.mll",q=1 ,l=1 ):#加载redshift
		cmds.evalDeferred('cmds.loadPlugin ("redshift4maya.mll")')
	#cmds.evalDeferred("""mel.eval('loadNewShelf "shelf_RareWolf_Ani.mel"')""")
	
cmds.evalDeferred('DelayedStart()')